home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / NT / CODE / CHAP08 / PAINT4 / PAINT4VIEW.CPP < prev    next >
C/C++ Source or Header  |  1996-04-05  |  2KB  |  101 lines

  1. //***********************************************************************
  2. //
  3. //  Paint4View.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "CLine.h"
  10. #include "Paint4Doc.h"
  11. #include "Paint4View.h"
  12.  
  13. IMPLEMENT_DYNCREATE (CPaintView, CScrollView)
  14.  
  15. BEGIN_MESSAGE_MAP (CPaintView, CScrollView)
  16.     ON_WM_LBUTTONDOWN ()
  17.     ON_WM_MOUSEMOVE ()
  18.     ON_WM_LBUTTONUP ()
  19.     ON_WM_CONTEXTMENU ()
  20. END_MESSAGE_MAP ()
  21.  
  22. void CPaintView::OnInitialUpdate ()
  23. {
  24.     SetScrollSizes (MM_TEXT, CSize (2048, 2048));
  25.     CScrollView::OnInitialUpdate ();
  26. }
  27.  
  28. void CPaintView::OnDraw (CDC* pDC)
  29. {
  30.     CPaintDoc* pDoc = GetDocument ();
  31.     int nCount = pDoc->GetLineCount ();
  32.  
  33.     if (nCount) {
  34.         for (int i=0; i<nCount; i++)
  35.             pDoc->GetLine (i)->Draw (pDC);
  36.     }
  37. }
  38.  
  39. void CPaintView::OnLButtonDown (UINT nFlags, CPoint point)
  40. {
  41.     CClientDC dc (this);
  42.     OnPrepareDC (&dc);
  43.     dc.DPtoLP (&point);
  44.  
  45.     m_ptFrom = point;
  46.     m_ptTo = point;
  47.     SetCapture ();
  48. }
  49.  
  50. void CPaintView::OnMouseMove (UINT nFlags, CPoint point)
  51. {
  52.     if (GetCapture () == this) {
  53.         CClientDC dc (this);
  54.         OnPrepareDC (&dc);
  55.         dc.DPtoLP (&point);
  56.  
  57.         InvertLine (&dc, m_ptFrom, m_ptTo);
  58.         InvertLine (&dc, m_ptFrom, point);
  59.         m_ptTo = point;
  60.     }
  61. }
  62.  
  63. void CPaintView::OnLButtonUp (UINT nFlags, CPoint point)
  64. {
  65.     if (GetCapture () == this) {
  66.         ReleaseCapture ();
  67.         CClientDC dc (this);
  68.         OnPrepareDC (&dc);
  69.         dc.DPtoLP (&point);
  70.         InvertLine (&dc, m_ptFrom, m_ptTo);
  71.  
  72.         CLine* pLine = GetDocument ()->AddLine (m_ptFrom, point);
  73.         if (pLine != NULL)
  74.             pLine->Draw (&dc);
  75.     }
  76. }
  77.  
  78. void CPaintView::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
  79. {
  80.     int nOldMode = pDC->SetROP2 (R2_NOT);
  81.  
  82.     pDC->MoveTo (ptFrom);
  83.     pDC->LineTo (ptTo);
  84.  
  85.     pDC->SetROP2 (nOldMode);
  86. }
  87.  
  88. void CPaintView::OnContextMenu (CWnd* pWnd, CPoint point)
  89. {
  90.     CMenu menu;
  91.     menu.LoadMenu (IDR_CONTEXTMENU);
  92.     CMenu* pContextMenu = menu.GetSubMenu (0);
  93.  
  94.     for (int i=0; i<8; i++)
  95.         pContextMenu->ModifyMenu (ID_COLOR_BLACK + i,
  96.             MF_BYCOMMAND | MF_OWNERDRAW, ID_COLOR_BLACK + i);
  97.  
  98.     pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
  99.         TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd ());
  100. }
  101.